package com.nexus.nsh.model.dao;

import java.util.logging.Logger;

import javax.jdo.PersistenceManager;

import com.nexus.nsh.exception.NSHModelException;
import com.nexus.nsh.model.PersistenceService;

public class GenericDao<T> {

	private Logger log;
	

	private PersistenceManager service;

	public GenericDao() {
		service = PersistenceService.get().getPersistenceManager();
		log  = Logger.getLogger(this.getClass().getName());
	}

	public void insert(T t) throws NSHModelException {
		log.info("Iniciando a camada de persistencia");
		
		try{
			
		service.makePersistent(t);
		
		} catch(Exception e){
			throw new NSHModelException(e);
		}
		log.info("Objeto persistido com sucesso");
	}

	public void delete(T t) {
		log.info("Iniciando a camada de persistencia");
		service.deletePersistent(t);
		log.info("Objeto excluido com sucesso");
	}

	public void updade(T t) {
		service.refresh(t);
	}

	@SuppressWarnings("unchecked")
	public T findById(Class c, long l) {
		return (T) service.getObjectById(c, l);
	}

}
